package com.lsq.tucao.express.service.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsq.tucao.express.bean.po.TucaoDetail;
import com.lsq.tucao.express.dao.TucaoDetailMapper;
import com.lsq.tucao.express.response.JsonResult;
import com.lsq.tucao.express.service.TucaoExpressService;
import com.lsq.tucao.express.utils.SensitiveWordFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
* @Author linshouqiang
* @Description service实现类
* @Date 2020/11/24 18:20
*/
@Slf4j
@Service
public class TucaoExpressServiceImpl implements TucaoExpressService {

    @Autowired
    private TucaoDetailMapper tucaoDetailMapper;
    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Override
    public JsonResult addTucao(TucaoDetail tucaoDetail) {
        if(tucaoDetail != null && !StringUtils.isEmpty(tucaoDetail.getContent())){
            //检测发布的吐槽中是否包含敏感词汇
            Set<String> set = sensitiveWordFilter.getSensitiveWord(tucaoDetail.getContent(), 1);
            if(set.size() > 0){
                return new JsonResult(500,"发布的吐槽中含有违禁词"+set);
            }else {
                int insert = tucaoDetailMapper.insert(tucaoDetail);
                if(insert > 0){
                    return new JsonResult(200,"发布吐槽成功");
                }else {
                    return new JsonResult(500,"发布吐槽失败");
                }
            }
        }else {
            return new JsonResult(500,"请输入要发布的吐槽内容");
        }
    }

//    @Override
//    public JsonResult likeTucao(Long tucaoId) {
//        if(tucaoId != null){
//            Integer update = tucaoDetailMapper.likeTucao(tucaoId);
//            if(update > 0){
//                return new JsonResult(200,"点赞成功");
//            }else {
//                return new JsonResult(500,"点赞失败");
//            }
//        }else {
//            return new JsonResult(500,"请传入要点赞的吐槽Id");
//        }
//    }

    @Override
    public JsonResult getPageTucao(Integer current, Integer size, String sort, Integer verify, Boolean getVerified) {
        Page<TucaoDetail> page = new Page<>(current,size);
        QueryWrapper<TucaoDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TucaoDetail::getIsDelete,1)
                .orderByDesc(TucaoDetail::getCreateTime);

        if(verify != null){
            queryWrapper.lambda().eq(TucaoDetail::getVerify,verify);
        }
        if(sort != null){
            queryWrapper.lambda().eq(TucaoDetail::getSort,sort);
        }

        if(getVerified){
            queryWrapper.lambda().ge(TucaoDetail::getVerify,1);
        }

        Page<TucaoDetail> tucaoDetailPage = tucaoDetailMapper.selectPage(page, queryWrapper);
        if(tucaoDetailPage != null){
            return new JsonResult(tucaoDetailPage,200,"分页查询吐槽数据成功");
        }else {
            return new JsonResult(500,"分页查询吐槽数据失败");
        }
    }

    @Override
    public JsonResult getTucaoById(Long tucaoId) {
        if(!StringUtils.isEmpty(tucaoId+"")){
            TucaoDetail tucaoDetail = tucaoDetailMapper.selectById(tucaoId);
            if(tucaoDetail != null && tucaoDetail.getIsDelete() == 1){
                return new JsonResult(tucaoDetail,200,"查询成功");
            }else {
                return new JsonResult(500,"查询失败");
            }
        }else {
            return new JsonResult(500,"请传入有效的id值");
        }
    }

    @Override
    public JsonResult verifyTucao(Long tucaoId, Boolean verify) {
        if(!StringUtils.isEmpty(tucaoId+"")){
            if(verify != null){
                UpdateWrapper<TucaoDetail> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(TucaoDetail::getTucaoId,tucaoId);
                //审核通过：1；审核未通过：2
                if(verify){
                    updateWrapper.lambda().set(TucaoDetail::getVerify,1);
                }else {
                    updateWrapper.lambda().set(TucaoDetail::getVerify,2);
                }
                int update = tucaoDetailMapper.update(null, updateWrapper);
                if(update > 0){
                    return new JsonResult(200,"审核成功");
                }else {
                    return new JsonResult(500,"审核失败");
                }
            }else {
                return new JsonResult(500,"请传入审核结果（verify）");
            }
        }else {
            return new JsonResult(500,"请传入有效的id值");
        }
    }

    @Override
    public JsonResult searchTucaoByUsername(Integer current, Integer size, String username, String sort) {
        if(!StringUtils.isEmpty(username)){
            Page<TucaoDetail> tucaoPage = new Page<>(current,size);
            QueryWrapper<TucaoDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TucaoDetail::getIsDelete,1)
                    .eq(TucaoDetail::getVerify,1)
                    .eq(TucaoDetail::getSort,sort)
                    .like(TucaoDetail::getUsername,username);

            Page<TucaoDetail> tucaoDetailPage = tucaoDetailMapper.selectPage(tucaoPage,queryWrapper);
            if (tucaoDetailPage != null && tucaoDetailPage.getRecords().size() > 0) {
                return new JsonResult(tucaoDetailPage,200,"搜索吐槽成功！");
            }else {
                return new JsonResult(500,"未搜索到相关用户发布的吐槽");
            }
        }else {
            return new JsonResult(500,"请输入要查询的用户名");
        }
    }

    @Override
    public JsonResult searchTucaoByContent(Integer current, Integer size, String content, String sort) {
        if(!StringUtils.isEmpty(content) && !StringUtils.isEmpty(sort)){
            Page<TucaoDetail> tucaoPage = new Page<>(current,size);
            QueryWrapper<TucaoDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TucaoDetail::getIsDelete,1)
                    .eq(TucaoDetail::getVerify,1)
                    .eq(TucaoDetail::getSort,sort)
                    .like(TucaoDetail::getContent,content);

            Page<TucaoDetail> tucaoDetailPage = tucaoDetailMapper.selectPage(tucaoPage,queryWrapper);
            if (tucaoDetailPage != null && tucaoDetailPage.getRecords().size() > 0)
            {
                return new JsonResult(tucaoDetailPage,200,"搜索吐槽成功！");
            }else {
                return new JsonResult(500,"未搜索到相关吐槽");
            }
        }else {
            return new JsonResult(500,"请输入要查询的吐槽内容");
        }
    }

    @Override
    public JsonResult sortTucaoBy(Integer current, Integer size, String sortBy, String sort) {
        if(!StringUtils.isEmpty(sortBy)){
            Page<TucaoDetail> tucaoPage = new Page<>(current,size);
            QueryWrapper<TucaoDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TucaoDetail::getIsDelete,1)
                    .eq(TucaoDetail::getVerify,1)
                    .eq(TucaoDetail::getSort,sort);

            if(sortBy.equals("按发布时间升序")){
                queryWrapper.lambda().orderByAsc(TucaoDetail::getCreateTime);
            }else if(sortBy.equals("按发布时间降序")){
                queryWrapper.lambda().orderByDesc(TucaoDetail::getCreateTime);
            }else if(sortBy.equals("按点赞数升序")){
                queryWrapper.lambda().orderByAsc(TucaoDetail::getLikeNum);
            }else if(sortBy.equals("按点赞数降序")){
                queryWrapper.lambda().orderByDesc(TucaoDetail::getLikeNum);
            }else if(sortBy.equals("按评论数升序")){
                queryWrapper.lambda().orderByAsc(TucaoDetail::getDiscussNum);
            }else if(sortBy.equals("按评论数降序")){
                queryWrapper.lambda().orderByDesc(TucaoDetail::getDiscussNum);
            }else {
                return new JsonResult(500,"请传入有效的筛选值");
            }

            Page<TucaoDetail> tucaoDetailPage = tucaoDetailMapper.selectPage(tucaoPage,queryWrapper);
            if(tucaoDetailPage != null)
            {
                return new JsonResult(tucaoDetailPage,200,"吐槽排序成功！");
            }else {
                return new JsonResult(500,"吐槽排序失败！");
            }
        }else {
            return new JsonResult(500,"请传入有效的筛选值");
        }
    }

}
